{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "30f21b67-73b7-4cad-94ad-de56e831c58a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#无人机模拟\n",
    "class UAVSimulator:\n",
    "    #初始化无人机\n",
    "    def __init__(self):\n",
    "        self.is_flying = False\n",
    "        self.position = [0, 0, 0]\n",
    "\n",
    "    #无人机起飞指令\n",
    "    def takeoff(self):\n",
    "        if not self.is_flying:\n",
    "            self.is_flying = True\n",
    "            self.position[2] = 10\n",
    "            return \"UAV has taken off and is at an altitude of 10 meters.\"\n",
    "        return \"UAV is already in the air.\"\n",
    "\n",
    "    #无人机着陆指令\n",
    "    def land(self):\n",
    "        if self.is_flying:\n",
    "            self.is_flying = False\n",
    "            self.position[2] = 0\n",
    "            return \"UAV has landed safely.\"\n",
    "        return \"UAV is already on the ground.\"\n",
    "\n",
    "    #无人机移动指令\n",
    "    def move(self, direction, distance):\n",
    "        if not self.is_flying:\n",
    "            return \"UAV is on the ground. Take off first to move.\"\n",
    "\n",
    "        #映射方向与位移\n",
    "        direction_map = {\n",
    "            'forward': (1, 0, 0),\n",
    "            'backward': (-1, 0, 0),\n",
    "            'left': (0, -1, 0),\n",
    "            'right': (0, 1, 0),\n",
    "            'up': (0, 0, 1),\n",
    "            'down': (0, 0, -1)\n",
    "        }\n",
    "\n",
    "        if direction in direction_map:\n",
    "            self.position = [self.position[i] + direction_map[direction][i] * distance for i in range(3)]\n",
    "            return f\"UAV moved {direction} by {distance} meters.\"\n",
    "        return f\"Unknown direction '{direction}'.\"\n",
    "\n",
    "    def get_status(self):\n",
    "        return f\"UAV status: {'Flying' if self.is_flying else 'On ground'}, Position: {self.position}\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f77eb528-97d9-40f6-acb0-8c25e67f4a23",
   "metadata": {},
   "outputs": [],
   "source": [
    "#提取输入的操作\n",
    "def parse_command(input_text):\n",
    "    if \"takeoff\" in input_text.lower():\n",
    "        return \"takeoff\", None, None\n",
    "    elif \"land\" in input_text.lower():\n",
    "        return \"land\", None, None\n",
    "    #传入格式为 move direction distance\n",
    "    elif \"move\" in input_text.lower():\n",
    "        for direction in ['forward', 'backward', 'left', 'right', 'up', 'down']:\n",
    "            if direction in input_text.lower():\n",
    "                words = input_text.lower().split()\n",
    "                for i in range(len(words)):\n",
    "                    if words[i] == direction:\n",
    "                        try:\n",
    "                            distance = int(words[i + 1])\n",
    "                            return \"move\", direction, distance\n",
    "                        except:\n",
    "                            return \"move\", direction, 1\n",
    "    return None, None, None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6d596774-e94c-4315-a0c2-709cb3fcf645",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Welcome to the UAV Simulator!\n",
      "UAV status: On ground, Position: [0, 0, 0]\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Enter command for the UAV:  quit\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Exiting the UAV Simulator. Goodbye!\n"
     ]
    }
   ],
   "source": [
    "def main():\n",
    "    UAV = UAVSimulator()\n",
    "    print(\"Welcome to the UAV Simulator!\")\n",
    "    print(UAV.get_status())\n",
    "\n",
    "    while True:\n",
    "        user_input = input(\"Enter command for the UAV: \")\n",
    "        \n",
    "        action_type, direction, distance = parse_command(user_input)\n",
    "        \n",
    "        if action_type == \"takeoff\":\n",
    "            print(UAV.takeoff())\n",
    "        elif action_type == \"land\":\n",
    "            print(UAV.land())\n",
    "        elif action_type == \"move\":\n",
    "            print(UAV.move(direction, distance))\n",
    "        elif user_input.lower() in [\"exit\", \"quit\"]:\n",
    "            print(\"Exiting the UAV Simulator. Goodbye!\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"Unknown command. Please try again.\")\n",
    "        \n",
    "        print(UAV.get_status())\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c89bd3ec-bbd8-4e82-a343-f0c8103afc62",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
